$(function () {

    $(".into span[class]").css("color", "#f07e0d");
    $("#memberRecharge .into span[class]").css("font-size", "20px");

    var memberList_dataGrid = $("#memberList_dataGrid");
    var memberListDialog = $("#memberListDialog");

    memberList_dataGrid.datagrid({
        url: "/member/list",
        fitColumns: true,//自动填充
        frozenColumns: {field: 'id', title: 'ID', width: 100, align: 'center'},   //锁定id列
        striped: true,       //显示斑马线
        columns: [[          //表头数据,可以有多行
            {field: 'xx', checkbox: true},
            {field: 'memberNumber', title: '卡号', width: 100, align: 'center'},
            {
                field: 'memberName',
                title: '会员名称',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    var sex = row.sex == 0 ? "先生" : "女士"
                    return value + "(" + sex + ")";
                }
            },
            {
                field: 'label', title: '标签', width: 50, align: 'center', formatter: function (value, row, index) {
                if (value == 0) {
                    return "";
                } else {
                    return '<img src="/static/images/' + value + 'rank.jpg" alt="' + value + '级" height="20px">';
                }
            }
            },
            {
                field: 'grade', title: '会员等级', width: 100, align: 'center', formatter: function (value, row, index) {
                return value ? value.name : "暂无";
            }
            },
            {field: 'integral', title: '可用积分', width: 100, align: 'center'},
            {field: 'balance', title: '储值余额', width: 100, align: 'center'},
            {field: 'totalConsume', title: '累计消费', width: 100, align: 'center'},
        ]],
        selectOnCheck: true,
        pagination: true,   //显示底部分页栏
        singleSelect: true, //只选择一行
        onDblClickRow: function (index, row) {
            memberListDialog.dialog("close");
            $("#memberId").val(row.id);
            $("#selectMember").textbox("setValue", row.memberNumber);
            $("#memberRecharge .memberName").text(row.memberName);
            $("#memberRecharge .grade").text(row.grade.name);
            $("#memberRecharge .birthDate").text(row.birthDate);
            $("#memberRecharge .balance").text(row.balance);
            $("#memberBalance").val(row.balance);
            $("#memberRecharge .integral").text(row.integral);
            $("#memberRecharge .totalConsume").text(row.totalConsume);
        }
    });
    $("#memberRecharge_dataGrid").datagrid({
        url: "/memberRecharge/list",
        fitColumns: true,//自动填充
        frozenColumns: {field: 'id', title: 'ID', width: 100, align: 'center'},   //锁定id列
        striped: true,       //显示斑马线
        columns: [[          //表头数据,可以有多行
            {field: 'xx', checkbox: true},
            {
                field: 'memberNumber',
                title: '卡号',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    return row.member.memberNumber;
                }
            },
            {
                field: 'memberName',
                title: '会员名称',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    return row.member.memberName;
                }
            },
            {
                field: 'operateType',
                title: '操作类型',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    if (value == 0) {
                        return "充值";
                    }
                    return "消费";
                }
            },
            {
                field: 'paymentWay',
                title: '操作类型',
                width: 50,
                align: 'center',
                formatter: function (value, row, index) {
                    return value.name;
                }
            },
            {field: 'beginMoney', title: '变动前金额', width: 50, align: 'center'},
            {
                field: 'money', title: '充值金额', width: 100, align: 'center', formatter: function (value, row, index) {
                if (row.operateType == 0) {
                    return "￥ +" + value;
                }
                return "￥ -" + value;
            }
            },
            {field: 'sendMoney', title: '赠送金额', width: 100, align: 'center'},
            {field: 'endMoney', title: '变动后金额', width: 100, align: 'center'},
            {field: 'operateDate', title: '充值时间', width: 100, align: 'center'},
            {field: 'remark', title: '备注', width: 100, align: 'center'},
        ]],
        selectOnCheck: true,
        pagination: true,   //显示底部分页栏
        singleSelect: true, //只选择一行
        onDblClickRow: function (index, row) {
            memberListDialog.dialog("close");
            $("#memberId").val(row.id);
            $("#selectMember").textbox("setValue", row.memberNumber);
            $("#memberRecharge .memberName").text(row.memberName);
            $("#memberRecharge .grade").text(row.grade.name);
            $("#memberRecharge .birthDate").text(row.birthDate);
            $("#memberRecharge .balance").text(row.balance);
            $("#memberBalance").val(row.balance);
            $("#memberRecharge .integral").text(row.integral);
            $("#memberRecharge .totalConsume").text(row.totalConsume);
        }
    });

    //充值菜单下的事件
    var objRecharge = {
        //选择会员
        selectMember: function () {
            memberListDialog.dialog("center", "none")
            memberListDialog.dialog("open")
        },
        addRecharge: function () {
            var memberId = $("#memberId").val() || null;
            if (memberId == null) {
                $.messager.alert('温馨提示', '请选择一个会员');
                return;
            }
            var selectMember = $("#memberRecharge #selectMember").textbox("getValue");
            var memberName = $("#memberRecharge .memberName").text();
            var paymentWay = $("#memberRecharge [name=paymentWay]").val();
            if (paymentWay == 1) {
                paymentWay = "微信";
            } else if (paymentWay == 2) {
                paymentWay = "支付宝";
            } else if (paymentWay == 3) {
                paymentWay = "银行卡";
            } else {
                paymentWay = "现金";
            }
            var operateType = $("#memberRecharge [name=operateType]").val();
            if (operateType == 1) {
                operateType = "付费";
            } else {
                operateType = "充值";
            }
            var money = $("#memberRecharge .money").textbox("getValue");
            var sendMoney = $("#memberRecharge .sendMoney").textbox("getValue");
            $.messager.confirm('确认',
                '你确定要为卡号为:' + selectMember + '的会员' + memberName + '以' + paymentWay + '的方式' + operateType + money + '并赠送' + sendMoney + '元吗？', function (r) {
                    if (r) {
                        $("#formRecharge").form("submit", {
                            url: "/memberRecharge/save",
                            success: function (data) {
                                data = JSON.parse((data));
                                if (data.success) {
                                    $.messager.alert('温馨提示', operateType + '成功');
                                    $("#formRecharge").form("reset", "none");
                                    //清除会员信息
                                    $("#selectMember").textbox("setValue", "");
                                    $("#memberRecharge .memberName").text("");
                                    $("#memberRecharge .grade").text("");
                                    $("#memberRecharge .birthDate").text("");
                                    $("#memberRecharge .balance").text("");
                                    $("#memberRecharge .integral").text("");
                                    $("#memberRecharge .totalConsume").text("");
                                    memberList_dataGrid.datagrid("load")
                                } else {
                                    $.messager.alert('温馨提示', operateType + '失败,原因:' + data.msg);
                                }
                            }
                        })
                    }
                });
        }
    }
    $("#memberRecharge [data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        objRecharge[cmd]();
    })

    //为充值金额和赠送金额绑定值改变事件,计算 小计
    $("#memberRecharge .money,.sendMoney").textbox({
        onChange: function () {
            var money = parseInt($("#memberRecharge .money").textbox("getValue") || 0);
            var sendMoney = parseInt($("#memberRecharge .sendMoney").textbox("getValue") || 0);
            $("#memberRecharge .summation").text(sendMoney + money);
        }
    });
    //选择会员弹窗
    $("#selectMember").textbox({
        buttonText: '搜索',
        onClickButton: function () {
            objRecharge.selectMember()
        }
    })
    //获取会员列表
    memberListDialog.dialog({
        title: "双击选择该会员",
        width: 700,
        height: 400,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closed: true,
        draggable: false,
        resizable: false,
        constrain: true
    });


    $(".paymentWay").combobox({
        url: '/dataDictionaryItem/getItem',
        valueField: 'id',
        textField: 'name',
        width: 200,
        value:7,
        editable: false,
        panelMaxHeight: 200,
        panelHeight: "auto",
        queryParams: {
            sn: 'paymentWay'
        }
    });
})

$(function () {

    //冲次弹窗
    var memberTimeListDialog = $("#memberTimeListDialog");
    //弹窗中的选择会员
    var memberTime_dataGrid = $("#memberTime_dataGrid");
    //会员次数表
    var TimeTable = $("#TimeTable");
    //会员次数明细表
    var TimeItemTable = $("#TimeItemTable");
    //商品表显示
    var departmentDialog_dataGrid = $("#departmentDialog_dataGrid");
    //商品列表弹窗
    var departmentDialog = $("#departmentDialog");

    //点击时加载数据
    $("#tt").tabs({
        onSelect: function (title, index) {
            if (index == 1) {
                TimeItemTable.datagrid({
                    url: "memberTimeItem/list"
                })
            }
        }
    });

    //计算小计价格
    $("#memberTime .number").textbox({
        onChange: function () {
            objTime.quadrature();
        }
    });
    var objTime = {
        //选择会员
        selectMember: function () {
            var keyword = $("#selectTime").textbox("getValue") || null;
            memberTime_dataGrid.datagrid({
                url: "/member/list",
                queryParams: {
                    keyword: keyword
                }
            })
            //获取框
            memberTimeListDialog.dialog("center", "none")
            memberTimeListDialog.dialog("open")
        },
        //弹出服务页面
        serviceShow: function () {
            var keyword = $("#serviceName").textbox("getValue") || null;
            departmentDialog_dataGrid.datagrid({
                url: "/productService/list",
                queryParams: {
                    keyword: keyword
                }
            })
            //获取框
            departmentDialog.dialog("center", "none")
            departmentDialog.dialog("open")
        },
        //添加会员冲次明细
        addProduct: function () {
            var TimeMemberId = $("#TimeMemberId").val() || null;
            if (TimeMemberId == null) {
                $.messager.alert('温馨提示', '请选择一个会员');
                return;
            }
            var serviceId = $("#serviceId").val() || null;
            if (serviceId == null) {
                $.messager.alert('温馨提示', '请选择一个商品');
                return;
            }
            var number = $("#memberTime .number").textbox("getValue") || null;//次数
            if (number == null) {
                $.messager.alert('温馨提示', '请选择一个商品');
                return;
            }

            var selectTime = $("#selectTime").textbox("getValue");

            var memberName = $("#memberTime .memberName").text();
            var paymentWay = $("#memberTime .paymentWay").val();
            var summation = $("#memberTime .summation").text();
            if (paymentWay == 1) {
                paymentWay = "微信";
            } else if (paymentWay == 2) {
                paymentWay = "支付宝";
            } else if (paymentWay == 3) {
                paymentWay = "银行卡";
            } else {
                paymentWay = "现金";
            }
            var product = $("#serviceName").textbox("getValue");//商品服务

            var cruuentDate = $("#cruuentDate").datebox("getValue");
            cruuentDate = cruuentDate.replace(/-/g, '/'); // "2010/08/01";
            var date = new Date(cruuentDate);
            var endDate = parseInt($("#endDate").val());
            date.setMonth(date.getMonth() + endDate);
            var str = date.getFullYear() + '-'
                // 因为js里month从0开始，所以要加1
                + (parseInt(date.getMonth()) + 1) + '-'
                + date.getDate();

            $.messager.confirm('确认',
                '你确定要为卡号为:' + selectTime + '名为' + memberName + '的会员以' + summation + '元' + paymentWay + '的方式购买' + product + '商品' + number + '次,有效期至:' + str, function (r) {
                    if (r) {
                        $("#saveProductService").form("submit", {
                            url: "/memberTimeItem/save",
                            queryParams: {
                                'time.endDate': str
                            },
                            success: function (data) {
                                data = JSON.parse((data));
                                if (data.success) {
                                    $.messager.alert('温馨提示', '结账成功');
                                    $("#saveProductService").form("reset", "none");
                                    //清除会员信息
                                    $("#selectTime").textbox("setValue", "");
                                    $("#memberTime .memberName").text("");
                                    $("#memberTime .grade").text("");
                                    $("#memberTime .integral").text("");
                                    TimeTable.datagrid('loadData',{total:0,rows:[]})
                                    departmentDialog_dataGrid.datagrid("reload")
                                } else {
                                    $.messager.alert('温馨提示', '结账失败,原因:' + data.msg);
                                }
                            }
                        })
                    }
                });

        },
        //求积
        quadrature: function () {
            var paymentMoney = parseInt($("#memberTime .paymentMoney").textbox("getValue") || 0);
            var number = parseInt($("#memberTime .number").textbox("getValue") || 0);
            $("#memberTime .summation").text(paymentMoney * number);
        }
    }

    $("#memberTime [data-cmd]").click(function () {
        var cmd = $(this).data("cmd");
        objTime[cmd]();
    })

    //选择会员弹窗
    $("#selectTime").textbox({
        buttonText: '搜索',
        onClickButton: function () {
            objTime.selectMember()
        }
    })
    //使用on绑定 handle
    $("#memberTime").on("click", ".handle", function () {
        var row = $(this).data("row");
        $("#serviceId").val(row.service.id);
        $("#serviceName").textbox("setValue", row.service.name);
        $("#memberTime .paymentMoney").textbox("setValue", row.service.price);
        $("#cruuentDate").datebox("setValue", row.endDate);
        $("#timeId").val(row.id);
        $("#timeNumber").val(row.number);
        objTime.quadrature();
    })

    //点击选择商品弹窗弹窗
    $("#serviceName").textbox({
        buttonText: '搜索',
        onClickButton: function () {
            objTime.serviceShow();
        }
    })
    //底部表格
    TimeItemTable.datagrid({
        fitColumns: true,//自动填充
        frozenColumns: {field: 'id', title: 'ID', width: 100, align: 'center'},   //锁定id列
        striped: true,       //显示斑马线
        columns: [[          //表头数据,可以有多行
            {field: 'xx', checkbox: true},
            {field: 'serviceName', title: '服务产品名称', width: 100, align: 'center'},
            {field: 'beginTime', title: '充值前次数', width: 100, align: 'center'},
            {field: 'endTime', title: '充值后次数', width: 100, align: 'center'},
            {field: 'number', title: '充值次数', width: 100, align: 'center'},
            {
                field: 'paymentWay',
                title: '支付方式',
                width: 100,
                align: 'center',
                formatter: function (value, row, index) {
                    return value.name;
                }
            },
            {field: 'paymentMoney', title: '支付金额', width: 100, align: 'center'},
            {
                field: 'employee', title: '充值员工', width: 100, align: 'center', formatter: function (value, row, index) {
                if (value == null) {
                    return "";
                }
                return value.name;
            }
            },
            {
                field: 'time', title: '到期时间', width: 100, align: 'center', formatter: function (value, row, index) {
                return value.endDate;
            }
            },
            {field: 'remark', title: '备注', width: 100, align: 'center'}
        ]],
        selectOnCheck: true,
        pagination: true,   //显示底部分页栏
        singleSelect: true, //只选择一行
    });
    //左表格表格初始化
    TimeTable.datagrid({
        fitColumns: true,//自动填充
        frozenColumns: {field: 'id', title: 'ID', width: 100, align: 'center'},   //锁定id列
        striped: true,       //显示斑马线
        columns: [[          //表头数据,可以有多行
            {field: 'xx', checkbox: true},
            {field: 'id', title: '序号', width: 100, align: 'center'},
            {
                field: 'service', title: '项目名称', width: 100, align: 'center', formatter: function (value, row, index) {
                return value.name;
            }
            },
            {field: 'addUpTime', title: '累计充值', width: 100, align: 'center'},
            {field: 'number', title: '剩余次数', width: 100, align: 'center'},
            {field: 'endDate', title: '到期时间', width: 100, align: 'center'},
            {
                field: 'handle', title: '操作', width: 100, align: 'center', formatter: function (value, row, index) {

                return "<a href='javascript:;' data-row='" + JSON.stringify(row) + "' class='handle'>续充</a>";
            }
            },
        ]],
        selectOnCheck: true,
        pagination: true,   //显示底部分页栏
        singleSelect: true, //只选择一行
    });
    //选择会员表格
    memberTime_dataGrid.datagrid({
        fitColumns: true,//自动填充
        frozenColumns: {field: 'id', title: 'ID', width: 100, align: 'center'},   //锁定id列
        striped: true,       //显示斑马线
        columns: [[          //表头数据,可以有多行
            {field: 'xxx', checkbox: true},
            {field: 'memberNumber', title: '卡号', width: 100, align: 'center'},
            {field: 'memberName', title: '会员名称', width: 100, align: 'center'},
            {
                field: 'grade', title: '会员等级', width: 100, align: 'center',
                formatter: function (value, row, index) {
                    return value.name;
                }
            },
            {field: 'integral', title: '积分', width: 100, align: 'center'}
        ]],
        selectOnCheck: true,
        pagination: true,   //显示底部分页栏
        singleSelect: true, //只选择一行
        onDblClickRow: function (index, row) {
            $("#memberTimeListDialog").dialog("close");
            $("#TimeMemberId").val(row.id);
            $("#selectTime").textbox("setValue", row.memberNumber);
            $("#memberTime .memberName").text(row.memberName);
            $("#memberTime .grade").text(row.grade.name);
            $("#memberTime .integral").text(row.integral);
            TimeTable.datagrid({
                url: "memberTime/list",
                queryParams: {
                    memberId: row.id
                }
            })
        }
    });
    //选择商品表格
    departmentDialog_dataGrid.datagrid({
        fitColumns: true,//自动填充
        frozenColumns: {field: 'id', title: 'ID', width: 100, align: 'center'},   //锁定id列
        striped: true,       //显示斑马线
        columns: [[          //表头数据,可以有多行
            {field: 'xxx', checkbox: true},
            {field: 'id', title: '序号', width: 100, align: 'center'},
            {field: 'name', title: '商品名称', width: 100, align: 'center'},
            {field: 'sn', title: '编码', width: 100, align: 'center'},
            {field: 'price', title: '价格', width: 100, align: 'center'},
            {field: 'explain', title: '说明', width: 100, align: 'center'}
        ]],
        selectOnCheck: true,
        pagination: true,   //显示底部分页栏
        singleSelect: true, //只选择一行
        onDblClickRow: function (index, row) {
            departmentDialog.dialog("close");
            $("#serviceId").val(row.id);
            $("#serviceName").textbox("setValue", row.name);
            $("#memberTime .paymentMoney").textbox("setValue", row.price);
            objTime.quadrature();
        }
    });
    //获取会员列表弹窗
    memberTimeListDialog.dialog({
        title: "双击选择该会员",
        width: 700,
        height: 400,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closed: true,
        draggable: false,
        resizable: false,
        constrain: true
    });
    //获取商品列表弹窗
    departmentDialog.dialog({
        title: "双击选择该商品",
        width: 700,
        height: 400,
        collapsible: false,
        minimizable: false,
        maximizable: false,
        closed: true,
        draggable: false,
        resizable: false,
        constrain: true
    });
})